package com.mojo.concurrent.concurrent_01_thread;

import com.mojo.concurrent.concurrent_07_pool.FixedThreadPool;

import java.util.concurrent.*;

/**
 * Callable简单示例
 *
 * @author <a href="mailto:mojo_jj@163.com">Mojo</a>
 */
public class CallableDemo1 {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        Future<Object> future = executorService.submit(new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                Thread.sleep(5000L);
                return "成功";
            }
        });
        try {
            // 超时等待
            System.out.println("超时结果" + future.get(3000L, TimeUnit.MILLISECONDS));
            // 阻塞等待线程执行结果
            System.out.println("结果" + future.get());
        } catch (InterruptedException | ExecutionException | TimeoutException e) {
            throw new RuntimeException(e);
        }
        executorService.shutdownNow();
    }
}
